import { WebpackPluginInstance, Compiler, Compilation, NormalModule } from 'webpack'
import {forEach, map} from "lodash";

const pluginName = 'myPlugin'

interface MyPluginOption {
    resource?: string[]
}

export default class MyPlugin implements WebpackPluginInstance{
    option: MyPluginOption
    resourceMap: Map<string, {
        version: string,
        count: number
    }>
    constructor(option: MyPluginOption) {
        this.option = option;
        this.resourceMap = new Map<string, {version: string; count: number}>();
        forEach(
            option.resource,
            (name) => this.resourceMap.set(name, {
                version: '',
                count: 0
            })
        )
    }


    apply(compiler: Compiler) {
        console.log(compiler.options.mode)
        compiler.hooks.compilation.tap(
            pluginName,
            (compilation: Compilation) => {
                compilation.hooks.buildModule.tap(
                    pluginName,
                    (module: NormalModule) => {
                        console.log('buildModule')
                    }
                );
                compilation.hooks.optimizeDependencies.tap(
                    pluginName,
                    (modules: NormalModule[]) => {
                        console.log('optimizeDependencies')
                        console.log(map(
                            modules,
                            ({context}) => context,
                        ))
                        // if (/node_modules\\(.*)/.test(module.context)) {
                        //     const name = RegExp.$1;
                        //     const { version } = module.resourceResolveData.descriptionFileData;
                        //     if (this.resourceMap.has(name)) {
                        //         this.resourceMap.set(name, {
                        //             version,
                        //             count: this.resourceMap.get(name).count + 1,
                        //         })
                        //     }
                        //     console.log(module.resource)
                        //     console.log(module.resourceResolveData.descriptionFileData.version)
                        // }
                    }
                );
                compilation.hooks.succeedModule.tap(
                    pluginName,
                    (module: NormalModule) => {
                        console.log('succeedModule')
                        // if (/node_modules\\(.*)/.test(module.context)) {
                        //     const name = RegExp.$1;
                        //     const { version } = module.resourceResolveData.descriptionFileData;
                        //     if (this.resourceMap.has(name)) {
                        //         this.resourceMap.set(name, {
                        //             version,
                        //             count: this.resourceMap.get(name).count + 1,
                        //         })
                        //     }
                        //     console.log(module.resource)
                        //     console.log(module.resourceResolveData.descriptionFileData.version)
                        // }
                    }
                );
                compilation.hooks.finishModules.tap(
                    pluginName,
                    (module) => {
                        console.log('finishModules');
                        console.log(this.resourceMap)
                    }
                )
                compilation.hooks.failedModule.tap(
                    pluginName,
                    (module) => {
                        console.log('failedModule')
                    }
                )
            }
        )
    }
}
